1. /* snmath.h by K.Tsuru */
  2. /*********************
  3. SN library
  4. Mathematical functions since version 2.21
  5. Separate from "snfunc.h" for convenience.
  6. **********************/
  7. #ifndef SN_MATH_H
  8. #define SN_MATH_H
  9. #ifndef SN_FUNCTIONS_H
  10. #include "snfunc.h"
  11. #endif // SN_FUNCTIONS_H
  12. /////////////////// common class /////////////////
  13. // x to n-th power i.e. return x^n since version 2.30
  14. // unsigned long version
  15. template <class T> T PowUL(const T& x, ulong n) {
  16. if(!n) return T(1.0);
  17. T y(1), z(x);
  18. while(1){
  19. if( n & 1 ) y *= z;
  20. n /= 2;
  21. if( !n ) break;
  22. z *= z;
  23. }
  24. return y;
  25. }
  26. // long version
  27. template <class T> T PowL(const T& x, const long n) {
  28. if(!n) return T(1.0);
  29. T y(1), z(x);
  30. long m = labs(n);
  31. while(1){
  32. if( m & 1 ) y *= z;
  33. m /= 2;
  34. if( !m ) break;
  35. z *= z;
  36. }
  37. if(n > 0) return y;
  38. return 1/y;
  39. }
  40. /********************************************
  41. Tailor series expansion for sine and cosine since ver. 2.31
  42. Type : double, long double or SDouble can be used.
  43. for SDouble "eps" should be cast "SDouble(1.0e-240)"
  44. but do not recomend.
  45. Please use Sin(x) or Cos(x).
  46. *********************************************/
  47. template <class Type>Type sinTaylorSeries(const Type& x, const Type& eps){
  48. int k = 2;
  49. Type sum = x, xsq = x*x, term = x;
  50. do {
  51. term *= (-1)*xsq/((k+1)*k);
  52. sum += term;
  53. k += 2;
  54. } while (abs(term) > eps);
  55. return sum;
  56. }
  57. template <class Type>Type cosTaylorSeries(const Type& x, const Type& eps){
  58. int k = 1;
  59. Type sum = 1.0, xsq= x*x, term = 1.0;
  60. do {
  61. term *= (-1)*xsq/((k+1)*k);
  62. sum += term;
  63. k += 2;
  64. } while (abs(term) > eps);
  65. return sum;
  66. }
  67. ////////////// SLong class /////////////////
  68. // greatest common divisor
  69. SLong gcdL(const SLong& a, const SLong& b); // 2000
  70. inline SLong gcd(const SLong& a, const SLong& b) { return gcdL(a, b); }
  71. // least common multiple
  72. SLong lcmL(const SLong& a, const SLong& b); // 2008 since ver 2.8
  73. inline SLong lcm(const SLong& a, const SLong& b){ return lcmL(a, b); }
  74. SLong Lpow(const SLong& x, ulong n); // n-th power of x(x^n) 2002
  75. //10^n
  76. SLong Lpow10(long n); // "friend" is deleted since version 2.192
  77. /**********************************************
  78. It returns a SLong random number in radix.
  79. When size = 0 it is taken as minArraySize.
  80. If 'size' has a value is greater than the maximum
  81. size it is adjusted to the maximum one.
  82. ***********************************************/
  83. SLong LRand(uint size, fType radix = DRADIX); // 2003
  84. // square root of SLong integer N, q = [sqrt(N)]
  85. // sgn has sign of N - q*q, then sgn >= 0
  86. // If N is a square of an integer, sgn = 0.
  87. SLong LSqrt(const SLong& N, int* sgn = NULL); // 2004
  88. // return M^d mod n
  89. SLong MpowMod(const SLong& M, ulong d, const SLong& n);// 2005
  90. inline SLong Pow2(ulong p){ return Lpow(2, p); } // inline function since ver. 2.3
  91. // If m and n have a common divisor in type 2^b*R^r, they are divided by it.
  92. void ReduceByPow2Rdx(SLong& m, SLong& n, SLong* divisor = NULL);// 2007
  93. inline SLong Fact(ulong n) { return FactPF(n); } // factorial n! since version 2.21
  94. const ulong nMax_combL = 2000UL; // maximum value of n
  95. SLong combL(ulong n, ulong k); // combination number(binomial) nCk using SInteger arithmertic that is fast for small n, k 4100
  96. ulong combUL(ulong n, ulong k); // ulong version(an assistant function) n < 30 (32 bits)
  97. SLong comb(ulong n, ulong k); // combination number(binomial) nCk 4102
  98. inline SLong binomial(ulong n, ulong k) { return comb(n,k); }// same as above
  99. inline SLong perm(const ulong n, const ulong r) { return permL(n, r); } // permutation number nPr "PrimeFactor" method
  100. SLong EulerNum(uint n); // n-th Euler number 4101
  101. SLong TanCoeff(uint n); // n-th tangent coefficient 4104
  102. void TanCoeffFree();
  103. void TanCoeffTable(SNBlock <SLong>& T, uint N); //table of tangent coefficient 4105
  104. /////////////////// SFraction class /////////////////
  105. SFraction BernNum(uint n); // Bernouilli's number 7100
  106. void BernNumTable(SNBlock <SFraction>& Bn, uint N); // table of Bernouilli's number 7101
  107. // Digit figures of factorial n! by Stirling's formula
  108. unsigned long Stirling(unsigned long n); // ver. 2.17.1 change into "unsigned long" from "long".
  109. /////////////////// SDouble class /////////////
  110. // Convert SDouble to an approximate in double.
  111. // If set err = 0, return DBL_MAX for overflow or zero for underflow error in double.
  112. // Error ID(OVERFLOW_ERR or UNDERFLOW_ERR) can be obtained by SNManeger::SNError().
  113. double doubleD(const SDouble& sd, int err = 1);// convert to double 3001
  114. ldouble ldpow10(const int n); // since version 2.31
  115. ldouble doubleLD(const SDouble& sd, int err = 1); // convert to long double since 2.31
  116. SDouble Dpow(const SDouble& x, long n); //n-th power of x(x^n) 3002
  117. inline SDouble Dpown(const SDouble& x, long n) { return Dpow(x, n); } // since ver. 2.30
  118. SDouble DpowD(const SDouble& x, const SDouble& p); // x^p = exp(p*log(x)) for x > 0 3003
  119. inline SDouble Dpow10(long n) {
  120. SDouble u(1.0); return u.MultPow10(n);
  121. }
  122. // inline SDouble Dpow10(const SDouble& p); // 10^p since ver 2.30 see below for the definition
  123. /************************************************
  124. It returns a random number with radix=rdx and exponent=exp.
  125. Default value exp = 0, radix = DRADIX.
  126. If size = 0 or size is greater than maxmum size it is
  127. adujusted to the maximum size.
  128. *************************************************/
  129. SDouble DRand(uint size = 0, int exp = 0, fType rdx = DRADIX);// 3005 The order of argument is changed since ver. 2.30
  130. inline SDouble Drand(uint size = 0, int exp = 0, fType rdx = DRADIX) { // small capital version
  131. return DRand(size, exp, rdx);
  132. }
  133. /*-----------------
  134. divide x into integer and decimal part
  135. SDouble version of modf()
  136. x = ipart + dpart
  137. For example
  138. x = -1.5
  139. ipart = -1.0
  140. It returns -1.5 - (-1) = -0.5.
  141. --------------------*/
  142. SDouble Modf(const SDouble& x, SDouble& ipart); // 3008
  143. // SDouble version of fmod()
  144. SDouble Fmod(const SDouble& x, const SDouble& y, SDouble& ipart); // 3009
  145. // convert SDouble x to E form i.e. mant*10^exp
  146. // It returns mantissa.
  147. SDouble SDtoE_Form(const SDouble& x, long* exp);
  148. // square root
  149. SDouble RecSqrt(const SDouble& x); // 1/sqrt(x) 3006
  150. SDouble Sqrt(const SDouble& x); // sqrt(x) 3007
  151. // cubic root
  152. SDouble Cbrt(const SDouble& x); // x^(1/3) 3010
  153. // reciporocal cubic root
  154. SDouble RecCbrt(const SDouble& x); // x^(-1/3) 3011
  155. // quartic root
  156. SDouble Qrrt(const SDouble& a); // x^(1/4) 3012
  157. // reciporocal quartic root
  158. SDouble RecQrrt(const SDouble& a); // x^(-1/4) 3013
  159. // exponential and hyperbolic functions
  160. SDouble Expower(long n); // n-th power of e 3302
  161. /**************************************************************************
  162. trigonometric functions calculated by DRAIDX
  163. **************************************************************************/
  164. SDouble Acos(const SDouble& x); // 3101
  165. SDouble Asin(const SDouble& x); // 3102
  166. const uint thFigAtan = 400u; // threshold value
  167. inline SDouble Atan(const SDouble& x) { return (x.EffFig() > thFigAtan) ? AtanA(x) : AtanT(x); } // since version 2.30
  168. inline SDouble Cos(const SDouble& x) { return CosBS(x); } // since version 2.31
  169. inline SDouble Sin(const SDouble& x) { return SinBS(x); }// since version 2.31
  170. inline SDouble Tan(const SDouble& x) { return TanBS(x); }// since version 2.30
  171. // SDouble version of C's "double atan2(double y, double x)" arctan(y/x) 3110
  172. SDouble Atan2(const SDouble& y, const SDouble& x);
  173. /**************************************************************************
  174. It provides the exponential function exp(x).
  175. From the condition "exp(x) < DRADIX^DRADIX_EXP_MAX" the range of 'x' must be
  176. within
  177. |x| < DFIGURES*log(10)*DRADIX_EXP_MAX = 301759.17...
  178. to inline since version 2.21-2.30
  179. ***************************************************************************/
  180. const uint thFigExpx = 510u; // threshold value
  181. inline SDouble Exp(const SDouble& x) { // since ver 2.30
  182. return (x.EffFig() > thFigExpx ) ? ExpBS(x) : ExpDS(x);
  183. }
  184. inline SDouble Cosh(const SDouble& x) {
  185. return CoshBS(x);
  186. }
  187. inline SDouble Sinh(const SDouble& x) {
  188. return SinhBS(x);
  189. }
  190. inline SDouble Tanh(const SDouble& x){
  191. return TanhBS(x);
  192. }
  193. // inverse hyperbolic functions
  194. SDouble Acosh(const SDouble& x); // 3309
  195. SDouble Asinh(const SDouble& x); // 3310
  196. SDouble Atanh(const SDouble& x); // 3311
  197. // logalithm functions
  198. inline SDouble Log(const SDouble& x) { // log(x) since ver 2.30
  199. return LogE(x);
  200. }
  201. inline SDouble Log10(const SDouble& x){ return Log(x)*RecLog10(); } // log_10(x)
  202. // y = a^x, log y = x
  203. inline SDouble Pow(const SDouble &a, const SDouble &x) { return DpowD(a, x); }
  204. /***********************************************************************
  205. Prototype declaration of constant functions has a formula
  206. SDouble func(const SDouble* userV = NULL,SDouble (*pfCalcFunc)() = fname);
  207. userV : a value given by user
  208. pfCalcFunc : default function, NULL is ok. A funcion which is faster than that
  209. of SN library can be used.
  210. See also "SetConstByFile()" in below.
  211. ************************************************************************/
  212. SDouble E(const SDouble* e = NULL,
  213. SDouble (*pfCalcFunc)() = SNE); // base of natural logarithm 3521
  214. void EFree(); //free the momory of exp1
  215. uint ESize();
  216. SDouble Log10(const SDouble* ln10 = NULL,
  217. SDouble (*pfCalcFunc)() = SNLog10); // log(10.0) 3522
  218. uint Log10Size();
  219. void Log10Free();
  220. SDouble Pi(const SDouble* pi = NULL,
  221. SDouble (*pfCalcFunc)() = SNPi); // pi 3523
  222. uint PiSize();
  223. SDouble M2Pi(); // 2/pi 3506
  224. void M2PiFree(); //free memory of m2pi
  225. /** 1/e If "invE=true", it returns "1/E()" else evaluates by
  226. the series(BS method for test "RecE()*E()=1 ?"). 3507 **/
  227. SDouble RecE(bool invE=true);
  228. void RecEFree(); //free the memory of recE
  229. SDouble MPi2(); // pi/2 3511
  230. void MPi2Free(); //free the memory of mpi2
  231. SDouble MPi4(); // pi/4 3550
  232. void MPi4Free(); //free the memory of mpi4
  233. SDouble RecLog10(); // 1/log(10.0) 3551
  234. void recln10Free(); //free the memory of recln10
  235. inline SDouble Dpow10(const SDouble& p) { // 10^p since ver 2.30 It needs the definition "Log10()".
  236. return Exp(p * Log10()); // There is not "double pow10(double x);" in new gcc.
  237. }
  238. /////////////////// SComplex class /////////////////
  239. #ifndef SCOMPLEX_H
  240. #include "scomplex.h"
  241. #endif // SCOMPLEX_H
  242. inline SComplex Conj(const SComplex& z) {
  243. return SComplex(z.Real(), -z.Imag());
  244. } // to inline since version 2.21
  245. // |z| most simple. If you need more accurate value, please improve your precision or use CabsH(z) below.
  246. inline SDouble Cabs(const SComplex& z) { return Sqrt(z.Norm()); }
  247. SDouble CabsH(const SComplex& z); // |z| high precision version
  248. inline SDouble Arg(const SComplex& z) { return Atan2(z.Imag(), z.Real()); } // argument in xy plane
  249. inline SDouble Real(const SComplex& z){ return z.Real();} // real part
  250. inline SDouble Imag(const SComplex& z){ return z.Imag();} // imaginary part
  251. inline SDouble Re(const SComplex& z) { return z.Real();} // Re(z) real part
  252. inline SDouble Im(const SComplex& z) { return z.Imag();} // Im(z) imaginary part
  253. inline SDouble Norm(const SComplex& z){ return z.Norm(); } // square of the magnitude
  254. inline SComplex MultI(const SComplex& z) { return SComplex(-Im(z), Re(z)); } // i*z
  255. inline SComplex MultMI(const SComplex& z) { return SComplex( Im(z), -Re(z) ); } // -i*z = z/i
  256. // Convert SComplex to an approximate in complex <double>.
  257. inline complex <double> complexD(const SComplex &z) {
  258. return complex <double> (doubleD(z.Real()), doubleD(z.Imag()));
  259. }
  260. /*********************************
  261. SComplex mathematical functions
  262. Acooding to GCC's "complex.h" the initial letter
  263. of funtion name is 'C';
  264. **********************************/
  265. SComplex Csqrt(const SComplex& z); // sqrt(z) 9101
  266. inline SComplex Cpolar(const SDouble& r, const SDouble& angle) {// r*{cos(angle)+i*sin(angle)}
  267. // return SComplex( r*Cos(angle), r*Sin(angle) );
  268. return CpolarBS(r, angle);
  269. }
  270. SComplex Cpow(const SComplex& z, int n); // z^n 9103
  271. SComplex Cpow(const SComplex& x, const SDouble& y);// x^y 9104
  272. inline SComplex Cpow(const SComplex& x, double y) {
  273. return Cpow(x, (SDouble)y);
  274. }
  275. SComplex Cpow(const SDouble& x, const SComplex& y);// x^y 9105
  276. inline SComplex Cpow(double x, const SComplex& y) {
  277. return Cpow( (SDouble)x, y);
  278. }
  279. SComplex Cpow(const SComplex& x, const SComplex& y);// x^y 9106
  280. SComplex Cexp(const SComplex& z); // exp z 9107
  281. SComplex Clog(const SComplex& z); // log z 9108
  282. SComplex Ccosh(const SComplex& z); // cosh z 9109
  283. SComplex Csinh(const SComplex& z); // sinh z 9110
  284. SComplex Ctanh(const SComplex& z); // tanh z 9111
  285. SComplex Ccos(const SComplex& z); // cos z 9112
  286. SComplex Csin(const SComplex& z); // sin z 9113
  287. SComplex Ctan(const SComplex& z); // tan z 9114
  288. //The following are added since ver. 2.18.
  289. SComplex Cacos(const SComplex& z); // arccos z 9115
  290. SComplex Casin(const SComplex& z); // arcsin z 9116
  291. SComplex Catan(const SComplex& z); // arctan z 9117
  292. SComplex Cacosh(const SComplex& z); // arcosh z 9118
  293. SComplex Casinh(const SComplex& z); // arsinh z 9119
  294. SComplex Catanh(const SComplex& z); // artanh z 9120
  295. #endif // SN_MATH_H

snmath.h : last modifiled at 2017/10/13 11:31:50(13,526 bytes)
created at 2016/04/11 11:18:58
The creation time of this html file is 2017/10/13 11:35:20 (Fri Oct 13 11:35:20 2017).